home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / SMEGUPD1.ZIP / SMEG03.ZIP / SMEG.TXT < prev    next >
Text File  |  1995-05-19  |  26KB  |  609 lines

  1.           ┌──────────────────────────────────────────┐
  2.           │  SMEG v0.3  A Linkable Polymorph Engine  │
  3.           └──────────────────────────────────────────┘
  4.           (Simulated Metamorphic Encryption Generator)
  5.  
  6.          Programming and Documentation (C) The Black Baron 1994
  7.          ══════════════════════════════════════════════════════
  8.  
  9.  
  10.  
  11. FILES INCLUDED IN THIS PACKAGE:
  12. ───────────────────────────────
  13.  
  14.         SMEG    .OBJ    The linkable SMEG v0.3 engine
  15.         SMEG    .TXT    This documentation
  16.  
  17.         GENDEMO .COM    Generation difference demonstration program
  18.         TRIVIA  .COM    A trivial, non polymorphic .COM virus
  19.         TRIVSMEG.COM    Same as TRIVIA.COM but now polymorphic!
  20.  
  21.         GENDEMO .ASM    TASM/MASM source for GENDEMO .COM
  22.         TRIVIA  .ASM    TASM/MASM source for TRIVIA  .COM
  23.         TRIVSMEG.ASM    TASM/MASM source for TRIVSMEG.COM
  24.  
  25.         POLYMORF.TXT    General description of a polymorph engine
  26.  
  27.  
  28.  
  29. ASSUMPTIONS MADE FOR THE USE OF SMEG:
  30. ─────────────────────────────────────
  31.  
  32. Every effort has been made to make SMEG as easy as possible to link into your
  33. own code, however, due to the complexity of the engine it is assumed that you
  34. have at least a basic knowledge of 8086/88 assembler.  Those of you who don't
  35. will find this package of little use!  It should be noted that linking SMEG
  36. to someone else's code is not a trivial task, however, if you write your own
  37. code then linking should be fairly painless!
  38.  
  39. So, with the above in mind, no explanation of how to program in 8086/88 will
  40. be given in this documentation.  It is assumed that you understand what I am
  41. babbling on about!
  42.  
  43. Also, it would be wise to print this document.  This will make your life a
  44. lot easier when you start playing with SMEG.  This document contains no
  45. special printer formatting codes (other than TABS) and has not been formatted
  46. in any page like style.  It is just "As It Was Written".  So, printing it on
  47. fan-fold will be OK, but if your printer can only print one page at a time
  48. you should load this into your favourite text editor/word processor and
  49. format it into pages of your own preference.
  50.  
  51. In this document I have used the term ADDRESS to represent an OFFSET within a
  52. SEGMENT.  ADDRESS, therefore, means JUST the offset and NOT segment:offset!
  53.  
  54.  
  55.  
  56. SMEG v0.3 WHAT IS IT AND WHY v0.3?
  57. ──────────────────────────────────
  58.  
  59. SMEG v0.3 is a polymorph engine that can be linked into your code, it was
  60. designed for computer viruses but could be used for other things, to encrypt
  61. and provide a unique decryptor.  See the enclosed POLYMORF.TXT for a more
  62. detailed description if you really don't know what a polymorph engine is!
  63.  
  64. v0.3?  Well, this is the first version to be made available to "the public",
  65. there have been two versions prior to this, v0.1 used in my PATHOGEN virus and
  66. v0.2 used in my QUEEG virus.  "STOP!", I hear you cry; "DOESN'T THUNDERBYTES
  67. TBAV v6.20 FIND SMEG ENCRYPTED VIRUSES?", well, yes and no!  It claims to find
  68. all known polymorphs and any future ones by using a clever software tracer
  69. technique and, to give it it's due, it does find approximately 96% of all
  70. QUEEG (SMEG v0.2) infections!  However, it still missed 4%!
  71.  
  72. SMEG v0.3, however, contains more advanced "junk program generator" technology
  73. than versions 0.1 and 0.2!
  74.  
  75.  
  76. AN EXAMPLE:
  77. ───────────
  78.  
  79. I took QUEEG and replaced SMEG v0.2 with SMEG v0.3 but with this technology
  80. "turned off".  TBAV 6.20 detected 96% of infections; reporting "A SMEG
  81. ENCRYPTED VIRUS" has been found.  Turning on the technology and running
  82. the test again TBAV 6.20 detected 0%!  In fact, it didn't even try decrypting
  83. the infected files and 'Skipped' or 'Checked' them, just like non-infected
  84. programs!
  85.  
  86.  
  87. ──────────────────────────────────────────────────────────────────────────────
  88.  
  89.  
  90. NOTES ON ASSEMBLING AND LINKING:
  91. ────────────────────────────────
  92.  
  93. Wherever possible you should force your assembler to do multiple passes, to
  94. fix those annoying, redundant, NOPS in your code.  TASM has this ability,
  95. sadly MASM doesn't (at least my version doesn't!).  The source code provided
  96. in this package is TASM/MASM compatible, if you use a different assembler
  97. then you might find assembling the demonstration programs awkward!
  98.  
  99. On the subject of linking.  When linking SMEG to your code it's easier if it
  100. is linked last, I.E:
  101.  
  102.         LINK    MYVIRUS SOMEDATA MORECODE SMEG;
  103.  
  104. One VERY important point is that SMEG is designed to run as a TINY memory
  105. modeled piece of code, therefore, before calling ANY of the SMEG functions
  106. you MUST make sure all your segment registers are the same and SMEG is
  107. reachable in the current CODE SEGMENT, just like a .COM file.  To this end,
  108. after linking you must use EXE2BIN to convert to a .COM or use the /T option
  109. on TLINK and the later versions of Microsofts LINK.
  110.  
  111. Because SMEG uses INDEXED CALLS and so-on if you don't make sure all the
  112. segments are the same before calling any SMEG functions, unpredictable results
  113. can occur.
  114.  
  115. Any assembler/linker command line examples in this document will be based on
  116. TASM and TLINK.
  117.  
  118. SMEG.OBJ was produced with TASM v2.51 with all non-essential items (regards to
  119. linking) removed.
  120.  
  121. There is only one segment (the code seg) and it's declaration was as follows:
  122.  
  123. CODESG          SEGMENT BYTE PUBLIC
  124.  
  125. The assume line was as follows:
  126.  
  127.         ASSUME  CS:CODESG,DS:CODESG,ES:CODESG,SS:CODESG
  128.  
  129. All procedures are NEAR and there are NO segment overrides or self contained
  130. work areas.
  131.  
  132. There are just three PUBLIC labels, referring to the NEAR procedures:
  133.  
  134.         POLYMORPH
  135.         ENCRYPT
  136.         JUNK_GEN
  137.  
  138. Theses are described in full later on in this document.
  139.  
  140.  
  141. ──────────────────────────────────────────────────────────────────────────────
  142.  
  143.  
  144. DEMONSTRATION PROGRAMS
  145. ──────────────────────
  146.  
  147. Before I describe how to link SMEG into you own code you might like to try
  148. running one of the demo programs included in this package?  If so, read the
  149. descriptions below before exiting your text viewer/editor:
  150.  
  151.  
  152. GENDEMO.COM
  153. ───────────
  154.  
  155. GENDEMO is a small program that will generate a chosen amount of executable
  156. and encrypted .COM files.  When any of these generated .COM's is run you will
  157. see a short message.  It's main purpose is to demonstrate how each generation
  158. is VERY different from the last.  Each generated .COM consists of a randomly
  159. generated decryptor followed by a small program to display a message.  In each
  160. case this small message program and the message itself will be encrypted.
  161.  
  162. Enter GENDEMO from the DOS prompt.
  163.  
  164. You will be presented with a small menu requesting the number of generations
  165. you require, 10 - 10000.  Only choose 10000 if you have a large hard disk and
  166. lots of time!!  After selecting the number, GENDEMO will proceed to generate,
  167. it will inform you when it's finished and return you to the DOS prompt.  If
  168. you now do a DIR you will see your chosen number of .COM's like this:
  169.  
  170.         0000.COM
  171.         0001.COM
  172.         0002.COM   .....   Down to your chosen number - 1
  173.  
  174. You can run any of these by entering it's name at the DOS prompt, I.E:  0002
  175.  
  176. You might like to use a HEX editor (or DEBUG) to see how different each
  177. generation is.
  178.  
  179. The source code for GENDEMO is provided as GENDEMO.ASM, it's TASM/MASM
  180. compatible and you can examine it to see how each generation is produced by
  181. using SMEG's functions.  The source is not commented.
  182.  
  183. You can re-assemble and link to produce GENDEMO.COM as follows:
  184.  
  185.         TASM  /M2 GENDEMO
  186.         TLINK /T  GENDEMO SMEG;
  187.  
  188. If using MASM and LINK, you must EXE2BIN the resultant .EXE to produce:
  189. GENDEMO.COM
  190.  
  191.  
  192. TRIVIA.COM
  193. ──────────
  194.  
  195. TRIVIA is a small non-resident, direct action .COM virus.  It contains no
  196. disk damaging routines and only contains a trivial payload.  To infect a file
  197. with TRIVIA do the following:
  198.  
  199. Make a temporary sub-directory and copy TRIVIA.COM into this.  Next move to
  200. this sub-directory and use ATTRIB to set the READ ONLY flag on TRIVIA.COM,
  201. this is how TRIVIA marks infected files and doing this will stop TRIVIA from
  202. infecting itself!  Next copy a "victim" .COM file from your system into this
  203. temporary sub-directory.
  204.  
  205. Now enter TRIVIA at the DOS prompt.
  206.  
  207. Do a DIR and you should notice that your victim file has grown!  Also, if you
  208. do an ATTRIB you will see the READ ONLY flag has been set.  However, the time
  209. and date should remain unaltered.
  210.  
  211. TRIVIA will infect ONE and only ONE uninfected file in the CURRENT and only
  212. the CURRENT directory.  If no uninfected files can be found, no infection will
  213. occur. 
  214.  
  215. Therefore, executing an infected file causes other .COM's in the current
  216. directory to become infected, one by one.  Because TRIVIA is NON-RESIDENT it
  217. can only reproduce when you execute a previously infected file.
  218.  
  219. The payload contained in TRIVIA is very trivial.  If it's a FRIDAY the 13th
  220. (very original, eh!!?) and you run an infected .COM the .COM will not run,
  221. instead, you are returned to DOS with the message:
  222.  
  223. "This program requires Microsoft Windows."
  224.  
  225. The source code for TRIVIA is provided as TRIVIA.ASM, it's TASM/MASM
  226. compatible.  It doesn't use any special tricks and could be re-coded much
  227. tighter!!  Apologies to code purists for this!  The source is commented.
  228.  
  229. You can re-assemble and link to produce TRIVIA.COM as follows:
  230.  
  231.         TASM  /M2 TRIVIA
  232.         TLINK /T  TRIVIA;
  233.  
  234. If using MASM and LINK, you must EXE2BIN the resultant .EXE to produce:
  235. TRIVIA.COM
  236.  
  237. TRIVIA.COM is provided as a demo virus, try infecting a few .COM's and then
  238. scan them with TBAV and you will see that they are spotted as:
  239.  
  240. "POSSIBLY INFECTED BY AN UNKNOWN VIRUS", or something like that!
  241.  
  242.  
  243. TRIVSMEG.COM
  244. ────────────
  245.  
  246. TRIVSMEG is identical to TRIVIA.COM with the exception that the virus is now
  247. polymorphic.  Prepare a few "sacrificial goats" and infect them as described
  248. in the description of TRIVIA.COM.  Replacing, of course, all references to
  249. TRIVIA with TRIVSMEG!
  250.  
  251. Now try scanning them with TBAV.  Spot the difference?!!!
  252.  
  253. Examine each infected file and note how different each infected portion is.
  254.  
  255. The source code for TRIVSMEG is provided as TRIVSMEG.ASM, it's TASM/MASM
  256. compatible.  It doesn't use any special tricks, apart from SMEG functions, and
  257. could be re-coded much tighter!!  The source is commented.
  258.  
  259. You can re-assemble and link to produce TRIVSMEG.COM as follows:
  260.  
  261.         TASM  /M2 TRIVSMEG
  262.         TLINK /T  TRIVSMEG SMEG;     {Note the linking in of SMEG}
  263.  
  264. If using MASM and LINK, you must EXE2BIN the resultant .EXE to produce:
  265. TRIVSMEG.COM
  266.  
  267. Examine the .ASM for TRIVSMEG to get a feel for linking with the SMEG engine,
  268. but first read on and familiarise yourself with SMEG and it's idiosyncrasies!
  269.  
  270.  
  271. ──────────────────────────────────────────────────────────────────────────────
  272.  
  273.  
  274. USING SMEG.OBJ IN YOUR OWN CODE
  275. ───────────────────────────────
  276.  
  277. The remainder of this document is dedicated to describing how to use the SMEG
  278. polymorph engine in your own code.  Please note; the text has been written
  279. from a "Virus Writers" point of view, but SMEG can be used in non-viral code
  280. should you wish.  It is at this point that I must stress again; you will need
  281. at least a basic understanding of 8086/88 assembly language to make sense of
  282. the text that follows!
  283.  
  284. POINTS TO NOTE:
  285. ───────────────
  286.  
  287. SMEG uses NO instructions above standard 8086/88 and it's junk code generator
  288. and decryptors also use only the basic 8086/88 instruction set, thus insuring
  289. that SMEG and it's generated code will run on ALL PC's and compatibles, from
  290. humble XT's to Pentium "Dream Machines"!
  291.  
  292. SMEG contains NO disk I/O routines.  It's purpose is to generate decryptors
  293. and encrypt code/data.  Therefore, all reading/writing to disk must be done
  294. by your own code!
  295.  
  296. LINKING SMEG INTO YOUR OWN CODE:
  297. ────────────────────────────────
  298.  
  299. Actually linking the engine with your own code is a painless task, one point
  300. to remember is that SMEG should be the LAST thing linked on the linker
  301. command line.  This makes life a lot easier!  An example:
  302.  
  303.         LINK    MYVIRUS SOMEDATA MORECODE SMEG;
  304.  
  305. Note how SMEG was linked last?  If your linker contains fancy segment sorting
  306. options these should be turned OFF to ensure that the SMEG object code is,
  307. indeed, the last thing in your final program.  Linking SMEG last isn't
  308. strictly essential, however, this document has been written with the "linking
  309. SMEG last" method in mind!
  310.  
  311. To CALL any of the SMEG functions (there are only three!) from your code you
  312. must include the following EXTRN declarations, EXACTLY as shown:
  313.  
  314.         EXTRN   POLYMORPH : NEAR
  315.         EXTRN   ENCRYPT   : NEAR
  316.         EXTRN   JUNK_GEN  : NEAR
  317.  
  318. IMPORTANT NOTE:
  319. ───────────────
  320.  
  321. You MUST NOT call ENCRYPT or JUNK_GEN without having first called POLYMORPH,
  322. as the POLYMORPH function sets up various data items for use by the other two
  323. routines.  THIS IS VERY IMPORTANT!
  324.        ───────────────────────
  325.  
  326. ANOTHER IMPORTANT NOTE!
  327. ───────────────────────
  328.  
  329. Because SMEG contains calls via indexed tables etc, you MUST ensure that ALL
  330. segment registers are the same value (as in a .COM file), this value MUST be
  331. the same as the current CODE SEGMENT where SMEG resides.  Failure to ensure
  332. this will result in unpredictable results, possibly a system crash or worse!
  333.  
  334. All three routines require the BP register to point to a free area of memory
  335. containing AT LEAST 45 bytes.  This 45 byte space is all that is used by SMEG
  336. as work area.  There is more information on register usage in the function
  337. descriptions.
  338.  
  339. ONE MORE IMPORTANT NOTE BEFORE I DESCRIBE EACH FUNCTION!
  340. ────────────────────────────────────────────────────────
  341.  
  342. If you are planning to CALL any of the SMEG functions from your code and your
  343. code DOES NOT reside at the address it originally started life at, then you
  344. MUST relocate your segments and IP offset as if it did!  Again, this is
  345. because SMEG uses indexed calls that are calculated at LINK TIME and remain
  346. static. A common example here would be a virus.  A diagram explains this:
  347.  
  348.        YOUR ORIGINAL                             YOUR APPENDED
  349.        ─────────────                             ─────────────
  350. your virus       : 0100h - 02FFh        victim program      : 0100h - 2FFFh
  351. SMEG             : 0300h - xxxxh        your appended virus : 3000h - 32FFh
  352.                     SMEG                : 3300h - xxxxh
  353.                      
  354. Note that after appending your virus to the victim program all the static
  355. addresses contained within SMEG's call tables will be incorrect because at
  356. LINK TIME these tables were calculated relative to address 0300h, now they
  357. are all at 3300h but still relative to 0300h!!  Thus, calling any SMEG
  358. functions without first relocating would be disastrous!  When you relocate
  359. you MUST remember to relocate ALL of the segment registers.
  360.  
  361. If you are not planning on calling any SMEG functions, or indeed, if your
  362. encrypted code doesn't contain the SMEG engine then relocation is optional.
  363. Unless, of course, YOUR code requires it!
  364.  
  365. It should be noted that the decryptors generated by SMEG are "stand alone"
  366. and do not require a copy of SMEG to run.  An example here are the .COM files
  367. generated by the GENDEMO.COM demonstration program, each of the generated
  368. COM's doesn't contain SMEG, just the decryptor followed by the encrypted code.
  369.  
  370. Relocation, using the method I am about to describe (which is the same as the
  371. method used in TRIVSMEG and my other two viruses PATHOGEN and QUEEG), is a
  372. two stage affair.
  373.  
  374. RELOCATION, STAGE ONE:
  375. ──────────────────────
  376.  
  377. You must first make sure that the length of the file you are appending your
  378. virus onto is divisible by 16 (10h) exactly in other words it's length MOD 16
  379. equals zero or, to put it yet another way, rounded up to the nearest paragraph
  380. boundary!
  381.  
  382. The following snippet of code calculates this, call it with AX equal to the
  383. low word of the filesize, it returns the number of bytes you have to add to
  384. the file (between 1 and 15) to make it's length MOD 16 = 0.  It returns AX as
  385. zero if no padding is required.  Look in TRIVSMEG.ASM to see this in action:
  386.  
  387.     CALC_PAD:       AND     AX,15           ;Mask low nibble
  388.             JNZ     NEED_TO_PAD     ;If it isn't zero, must pad!
  389.             RET
  390.     NEED_TO_PAD:    NEG     AX              ;NEG AX
  391.             ADD     AX,16           ;Same as 16 - original AX
  392.             RET
  393.  
  394. I should mention here the size of ALL decryptors generated by SMEG MOD 16 = 0
  395. Therefore, after padding the file and appending a SMEG generated decryptor you
  396. still find yourself on a paragraph boundary, which is handy (and essential!)
  397. for the second stage of this type of relocation process!
  398.  
  399. RELOCATION, STAGE TWO:
  400. ──────────────────────
  401.  
  402. As mentioned earlier, you must ensure that ALL segments and your IP offset
  403. are the same as they were in your original, non-appended, code.  The following
  404. snippet of code will achieve this.  Again, look in TRIVSMEG.ASM to see it in
  405. action.  It should be noted, this code MUST be the VERY FIRST thing in your
  406. code, AFTER the decryptor, to ensure correct relocation:
  407.  
  408.     RELOCATE:       CALL    FETCH_IP        ;Call the next instruction
  409.     FETCH_IP:       POP     AX              ;Fetch address of here into AX
  410.             DEC     AH              ;SUB 256
  411.             MOV     CL,4            ;A Shift of 4
  412.             SHR     AX,CL           ;Divide AX by 16
  413.             MOV     BX,CS           ;Fetch current CODE SEGMENT
  414.             ADD     AX,BX           ;ADD adjustment to it
  415.             PUSH    AX              ;Stack relocated CODE SEGMENT
  416.             MOV     AX,RELOCATED    ;Fetch continuation address
  417.             PUSH    AX              ;Stack it
  418.             RETF                    ;FAR RETurn to the next line!
  419.     RELOCATED:                              ;Rest of your code here
  420.  
  421. After the RETF, continuation occurs at the address RELOCATED and the CS:IP
  422. is the same as if you had run your original code.  However, at this point all
  423. of the other segment registers remain as they were prior to the relocation. 
  424. If you are a virus you should make a note of one of these registers so you
  425. can restore all the original segment registers prior to running your host!
  426.  
  427. The first thing your code should do, after relocation, is to make sure all
  428. the other segment registers equal the new, relocated, CODE SEGMENT before you
  429. make any SMEG function calls.  As per SMEG's segment rules.
  430.  
  431. Note, in the case of a virus, the above relocation technique can be used for
  432. appending to .EXE's as well as .COM's
  433.  
  434. You may know a better way of relocation, if so then you can use that. 
  435. Providing, of course, SMEG's rules regarding relocation are adhered to.
  436.  
  437.  
  438. SMEG FUNCTION CALLS AND REQUIREMENTS:
  439. ─────────────────────────────────────
  440.  
  441. SMEG's functions will be described in order of relevance, which just so
  442. happens to be the CALL order too!
  443.  
  444. It should be noted here that SMEG is 2016 (07E0h) bytes in size.
  445. ────────────────────────────────────────────────────────────────
  446. The 45 byte work area used by the SMEG functions is disposable and doesn't
  447. have to travel with the engine.
  448.  
  449.  
  450. Function Name:  POLYMORPH
  451. ─────────────────────────
  452.  
  453. This function is the main function contained within SMEG.  It's purpose is to
  454. construct a random size decryptor containing a random decryptor with random
  455. register use peppered throughout random junk code.  This function also sets
  456. up the required values, in a workspace, for the other two SMEG functions and,
  457. therefore, MUST be called prior to calling the other functions.
  458.  
  459. To call this function you must set up the following registers and observe any
  460. RAM requirements:
  461.  
  462.     BP : Must point to an area of memory with 45 bytes free.
  463.          This is the above mentioned WORKSPACE.
  464.  
  465.     AX : Must hold the actual start address of the decryptor.  E.G.  If
  466.          you were generating a decryptor that is at the very start of a
  467.          .COM file you would load AX with 100h.  See GENDEMO.ASM for
  468.          this type of generation in action.  Likewise, for a decryptor
  469.          designed to be appended to some other piece of code you would
  470.          load AX with the append address.  Example:  You are appending a
  471.          decryptor onto a .COM file that is 300h bytes long, in this case
  472.          you would load AX with 400h (100h+size of file = append address)
  473.          See TRIVSMEG.ASM for this type of generation in action.
  474.  
  475.     CX : Must hold the length, in bytes, of the code/data you wish to
  476.          encrypt.
  477.  
  478.     DX : Must point to the start address of the code/data you wish to
  479.          encrypt.
  480.  
  481.     DI : Must point to an area of memory with at least CX (see above)
  482.          bytes of space free.  If CX is less than 1792 (700h) bytes then
  483.          there MUST be 1792 (700h) bytes free.  This space MUST NOT clash
  484.          with the 45 byte space pointed to by BP!
  485.  
  486. After calling polymorph the RAM pointed to by DI contains a random sized,
  487. random decryptor.  This can be written out to disk at this point as it is no
  488. longer needed by SMEG!  The size of this decryptor varies, from between 320
  489. (140h) and approximately (but no more than) 1792 (700h) bytes and is always
  490. rounded to the nearest paragraph.  To find out the true size of the decryptor
  491. (providing BP points to the 45 byte work area, and it still will if you
  492. haven't altered it!) just do a:
  493.  
  494.         MOV     wordreg,[BP+39]  (39 is decimal)
  495.  
  496. So, you would write this many bytes from the memory originally pointed to by
  497. DI.  You can find out the original address (DI when you called POLYMORPH) by:
  498.  
  499.         MOV     wordreg,[BP+4]  ;It's address   {DI originally}
  500.  
  501. On return from POLYMORPH all registers are destroyed, with the exception of:
  502. BP, BX, SI and the segment registers.
  503.  
  504.  
  505. Function Name:  ENCRYPT
  506. ───────────────────────
  507.  
  508. This function is called after calling POLYMORPH to encrypt your code/data
  509. ready for appending after the decryptor.  Note:  You should write out your
  510. decryptor to the file before calling ENCRYPT else your decryptor in RAM will
  511. be overwritten, as the same RAM area is used.
  512.  
  513. To call this function you must set up the following registers and observe any
  514. RAM requirements:
  515.  
  516.     BP : Must point to the same 45 byte work area it did prior to calling
  517.          POLYMORPH, as BP is preserved by POLYMORPH it's a good idea not
  518.          to alter it between POLYMORPHS return and calling ENCRYPT.
  519.  
  520. After calling ENCRYPT the RAM pointed to by DI (when you called POLYMORPH)
  521. will contain your encrypted code/data, it's size is the same as the value in
  522. then CX register when you called POLYMORPH.  This encrypted code/data can now
  523. be written to disk immediately after the decryptor.  Alternatively, you can
  524. recover the size of your encrypted code and it's address by:
  525.  
  526.         MOV     wordreg,[BP]    ;It's size      {CX originally}
  527.         MOV     wordreg,[BP+4]  ;It's address   {DI originally}
  528.  
  529. Again, this assumes BP still points to the 45 byte SMEG work area.
  530.  
  531. The preserved and destroyed registers are the same as those for POLYMORPH.
  532.  
  533. IMPORTANT:  The encrypted code/data MUST be written to the file IMMEDIATELY
  534.         after the decryptor, NO code/data is to be written in between.
  535.  
  536.  
  537. Function Name:  JUNK_GEN
  538. ────────────────────────
  539.  
  540. This function is a optional one.  It's purpose is to generate a random amount
  541. of junk that you can append after your encrypted code/data.  This makes
  542. finding the true end of your encrypted code/data impossible.
  543.  
  544. To call this function you must set up the following registers and observe any
  545. RAM requirements:
  546.  
  547.     BP : Must point to the same 45 byte work area it did prior to calling
  548.          POLYMORPH and ENCRYPT, as BP is preserved by both POLYMORPH and
  549.          ENCRYPT it's a good idea not to alter it between ENCRYPTS return
  550.          and calling JUNK_GEN.
  551.         
  552. After calling JUNK_GEN the RAM pointed to by DI (when you called POLYMORPH)
  553. will contain a random amount of random junk.  This junk can now be written to
  554. disk immediately after the decryptor, encrypted code/data.  The size of this
  555. junk will vary but is always at least 128 (80h) and no more than 1151 (47Fh)
  556. bytes.  You can, again, recover the original DI address by:
  557.  
  558.         MOV     wordreg,[BP+4]  ;It's address
  559.  
  560. The preserved and destroyed registers are the same as those for POLYMORPH and
  561. ENCRYPT with the exception of the following:
  562.  
  563.         DX      returns pointing to the start of the junk
  564.             the same as the original DI or the word [BP+4]
  565.         CX      returns holding the size of the junk
  566.  
  567. These just happen to be the registers required for a DOS write, and providing
  568. your file handle is in BX you can write out the junk to the file by simply:
  569.  
  570.         MOV     AH,40h
  571.         INT     21h
  572.  
  573. The size of this junk is pre-calculated during the call to POLYMORPH. 
  574. Therefore, after calling POLYMORPH you can find out how big this junk is
  575. going to be before it is actually generated by doing:
  576.  
  577.         MOV     wordreg,[BP+37]  (37 is decimal)
  578.  
  579. This is useful to know, for adjusting .EXE headers etc.  Remember, generating
  580. this junk and writing it to the file is optional and doesn't affect the
  581. decryptor in any way.
  582.  
  583. Well, that's the functions described.  A simple (pseudo code) example of an
  584. appending SMEG encryption session might look something like this:
  585.  
  586.         OPEN    FILE
  587.         GET     FILESIZE
  588.         PAD     SO FILESIZE MOD 16 = 0
  589.         SET     POLYMORPH REGISTERS
  590.         CALL    POLYMORPH
  591.         WRITE   DECRYPTOR TO THE FILE
  592.         CALL    ENCRYPT
  593.         WRITE   ENCRYPTED CODE TO THE FILE
  594.         CALL    JUNK_GEN         (optional)
  595.         WRITE   JUNK TO THE FILE (optional)
  596.         CLOSE   FILE
  597.  
  598. That's all there is to it!!!  I've tried to describe SMEG in a brief, but I
  599. hope, complete way.  If you are still unsure how to use it have a look at the
  600. source code for TRIVSMEG.ASM and GENDEMO.ASM (which are both supplied) as
  601. these two programs both use the SMEG engine.
  602.  
  603. Have fun with SMEG and all I ask is that should you give copies away to your
  604. friends please ONLY give them the archive that you received, I.E.  With NO
  605. amendments.  Thanks.  Oh, and maybe a little credit in your creation for me?!
  606.  
  607. (C) The Black Baron 1994.
  608.  
  609.